<?php
class ArticleHolder extends Page {
    
    static $db = array(
        'ArticlesPerPage' => 'Float'
    );
    
    static $icon = 'themes/default/images/icons/newspage';
    
    static $allowed_children = array("ArticlePage");
 
    static $default_child = "ArticlePage";
        
    function getCMSFields() {
        $fields = parent::getCMSFields();
         
        $fields->removefieldFromTab('Root.Content', 'Content');
        $fields->removefieldFromTab('Root.Content', 'Image');
        $fields->addFieldToTab('Root.Content.Main', new LiteralField('Content', '<br /><h2>De inhoud van deze pagina bestaat uit Nieuwsartikelen (subpagina\'s).</h2><p>Nieuwsartikelen worden automatisch gesorteerd op datum.<br />'));       
        $fields->addFieldToTab('Root.Content.Main', new DropdownField('ArticlesPerPage','Aantal Nieuwsartikelen per pagina', array('5' => '5', '10' => '10', '15' => '15', '20' => '20', '25' => '25' )));
        return $fields;
    }
    
    function getCustomSort() {
        return 'Date';
    }
    
}
 
class ArticleHolder_Controller extends Page_Controller {
     
    public static $allowed_actions = array (
    );

    public function init() {
	parent::init();
        RSSFeed::linkToFeed($this->Link()."rss", "Helemaal Herentals Nieuws");
        
        Requirements::customScript('
        
        (function($) {
            $(document).ready(function(){   
        
                $(".newsslide:first").show();
    
                $(".newsarticle a.clicknews").click(function(){
                    if($(this).closest(".newsarticle").find(".newsslide").is(":visible")) {
                        $(this).toggleClass("active").html("lees meer").closest(".newsarticle").find(".newsslide").fadeOut(500); 
                    } else {
                        $(this).toggleClass("active").html("verklein").closest(".newsarticle").find(".newsslide").slideDown(1000);
                    }
        
                    return false;
                });
            })
        })(jQuery);
        
        ');
        
        
    }
        
    public function SortedChildren(){
        
        $this->ArticlesPerPage ? $app = $this->ArticlesPerPage : $app = 15;
        
        if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
        
        $SQL_start = (int)$_GET['start'];
        
        return DataObject::get("ArticlePage", "ParentID = $this->ID", "Date DESC", Null, $SQL_start .", " .$app); 
    }
    
    function rss() {
        $rss = new RSSFeed($this->Children(), $this->Link(), "Helemaal Herentals Nieuws");
        $rss->outputToBrowser();
    }
}
